package cn.hongxin.sap.conn.controller;


import io.swagger.annotations.*;
import cn.getech.poros.framework.common.auditlog.AuditLog;
import cn.getech.poros.framework.common.auditlog.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import cn.getech.poros.framework.common.param.PageParam;
import cn.getech.poros.framework.common.api.PageResult;
import cn.getech.poros.framework.common.api.RestResponse;
import cn.getech.poros.framework.common.utils.ExcelUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import cn.hongxin.sap.conn.entity.PorosDemoUser;
import cn.hongxin.sap.conn.service.IPorosDemoUserService;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户控制器
 * </p>
 *
 * @author hengji.li@getech.cn
 * @since 2020-03-04
 */
@RestController
@RequestMapping("/porosDemoUser")
@Api(tags = "用户服务接口")
public class PorosDemoUserController {


    @Autowired
    private IPorosDemoUserService porosDemoUserService;

    /**
    * 获取全部用户列表
    */
    @ApiOperation("获取全部用户列表")
    @GetMapping(value = "/all")
    public RestResponse list() {
        return RestResponse.ok(porosDemoUserService.list());
    }

    /**
     * 分页获取用户列表
     */
    @ApiOperation("分页获取用户列表")
    @GetMapping("/list")
    public RestResponse pageList(PorosDemoUser porosDemoUser, @Valid PageParam pageParam){
        Wrapper wrapper = Wrappers.query(porosDemoUser);
        Page<PorosDemoUser> pageObj = porosDemoUserService.page(new Page<>(pageParam.getPageNo(),pageParam.getLimit()), wrapper);

        return RestResponse.ok(new PageResult(pageObj.getRecords(),pageObj.getTotal()));
    }

    /**
     * 新增用户
     */
    @ApiOperation("新增用户")
    @AuditLog(title = "用户",desc = "新增用户",businessType = BusinessType.INSERT)
    @PostMapping
    public RestResponse add(@RequestBody
            @Valid PorosDemoUser porosDemoUser) {
        return RestResponse.ok(porosDemoUserService.save(porosDemoUser));
    }


    /**
     * 修改用户
     */
    @ApiOperation(value="修改用户")
    @AuditLog(title = "用户",desc = "修改用户",businessType = BusinessType.UPDATE)
    @PutMapping
    public RestResponse update(@RequestBody @Valid PorosDemoUser porosDemoUser) {
        return RestResponse.ok(porosDemoUserService.updateById(porosDemoUser));
    }

    /**
     * 根据id删除用户
     */
    @ApiOperation(value="根据id删除用户")
    @AuditLog(title = "用户",desc = "用户",businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public RestResponse delete(@PathVariable Long[] ids) {
        return RestResponse.ok(porosDemoUserService.removeByIds(Arrays.stream(ids).collect(Collectors.toSet())));
    }


    /**
     * 根据id获取用户
     */
    @ApiOperation(value = "根据id获取用户")
    @GetMapping(value = "/{id}")
    public RestResponse get(@PathVariable  Long id) {
            return RestResponse.ok(porosDemoUserService.getById(id));
    }

    /**
     * 导出用户列表
     */
    @ApiOperation(value = "导出用户列表")
    @AuditLog(title = "用户",desc = "导出用户列表数据",businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(PorosDemoUser porosDemoUser, HttpServletResponse response){
        //Wrapper wrapper = Wrappers.query(porosDemoUser);
        List<PorosDemoUser> list = porosDemoUserService.list();
        ExcelUtils<PorosDemoUser> util = new ExcelUtils<PorosDemoUser>(PorosDemoUser.class);
        util.exportExcel(list, "用户",response);
    }
}
